package cn.com.git.admin.api;


import cn.com.git.admin.config.security.LoginContext;
import cn.com.git.admin.service.SystemService;
import cn.com.git.admin.service.dto.AuthenticationTokenDTO;
import cn.com.git.admin.service.dto.SysResourceDetailDTO;
import cn.com.git.admin.service.dto.SysResourceListDTO;
import cn.com.git.admin.service.dto.SysResourceSaveDTO;
import cn.com.git.admin.service.query.SysResourceListQuery;
import cn.com.git.common.base.BaseApi;
import cn.com.git.common.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统资源
 * @author zhangzhenfu
 * @Description
 * @date 2020/2/22 15:32
 */
@Api(value = "系统资源", description = "系统资源")
@RestController
@RequestMapping("/sys_resource")
public class SysResourceApi extends BaseApi {

    @Autowired
    private LoginContext loginContext;

    @Autowired
    private SystemService systemService;

    @ApiOperation(value = "列表查询", notes = "列表查询", httpMethod = "POST",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/list/page",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultPage<SysResourceListDTO> listPage(@Valid @RequestBody SysResourceListQuery query) {
        ResultPage<SysResourceListDTO> resultPage ;
        try {
            resultPage = systemService.listSysResource(query);
        }catch (Throwable e){
            resultPage = new ResultPage<>(ResultConstant.FAILED);
            logger.error("列表查询失败：{}",e.getMessage());
        }
        return resultPage ;
    }

    @ApiOperation(value = "列表查询", notes = "列表查询（树）", httpMethod = "POST",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/list/tree",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<List<DataTree>> listTree(@Valid @RequestBody SysResourceListQuery query) {
        AuthenticationTokenDTO authenticationDTO = loginContext.getCurrentUser();
        ResultMsg<List<DataTree>> result ;
        try {
            result = systemService.listTreeSysResource(query);
        }catch (Throwable e){
            result = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("列表（树）查询失败：{}",e.getMessage());
        }
        return result ;
    }


    @GetMapping(value = "/find",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询", httpMethod = "GET", notes = "根据id查询数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", paramType = "query", dataType = "integer"),
    })
    public ResultMsg<SysResourceDetailDTO> find(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg<SysResourceDetailDTO> resultMsg ;
        try {
            resultMsg = systemService.findSysResource(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("查询失败：{}",e.getMessage());
        }
        return resultMsg ;
    }


    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<SysResourceSaveDTO> add(@Valid @RequestBody SysResourceSaveDTO form) {
        ResultMsg<SysResourceSaveDTO> resultMsg = null ;
        try {
            form.getM().setState(CommonStateConstant.TRUE.getValue());
            resultMsg = systemService.addSysResource(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("新增失败：{}",e.getMessage());
        }
        return resultMsg ;
    }
    @ApiOperation(value = "编辑", notes = "编辑", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/edit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<SysResourceSaveDTO> edit(@Valid @RequestBody SysResourceSaveDTO form) {
        ResultMsg<SysResourceSaveDTO> resultMsg ;
        try {
            form.getM().setState(CommonStateConstant.TRUE.getValue());
            resultMsg = systemService.editSysResource(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("编辑失败：{}",e.getMessage());
        }
        return resultMsg ;
    }

    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除", httpMethod = "GET", notes = "根据id删除数据",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", paramType = "query", dataType = "integer"),
    })
    public ResultMsg delete(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg resultMsg ;
        try {
            resultMsg = systemService.deleteSysResource(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("删除失败：{}",e.getMessage());
        }
        return resultMsg ;
    }


}
